<template>
  <el-dialog
    :title="!dataForm.welderid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="焊工姓名" prop="weldername">
      <el-input v-model="dataForm.weldername" placeholder="焊工姓名"></el-input>
    </el-form-item>
    <el-form-item label="员工卡号" prop="weldercard">
      <el-input v-model="dataForm.weldercard" placeholder="员工卡号"></el-input>
    </el-form-item>
    <el-form-item label="所属部门" prop="dept">
      <el-input v-model="dataForm.dept" placeholder="所属部门"></el-input>
    </el-form-item>
    <el-form-item label="班组" prop="groupname">
      <el-input v-model="dataForm.groupname" placeholder="班组"></el-input>
    </el-form-item>
    <el-form-item label="技术级别" prop="techlevel">
      <el-input v-model="dataForm.techlevel" placeholder="技术级别"></el-input>
    </el-form-item>
    <el-form-item label="出生日期" prop="birthday">
      <el-input v-model="dataForm.birthday" placeholder="出生日期"></el-input>
    </el-form-item>
    <el-form-item label="卡号类型" prop="cardtype">
      <el-input v-model="dataForm.cardtype" placeholder="卡号类型"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          welderid: 0,
          weldername: '',
          weldercard: '',
          dept: '',
          groupname: '',
          techlevel: '',
          birthday: '',
          cardtype: ''
        },
        dataRule: {
          weldername: [
            { required: true, message: '焊工姓名不能为空', trigger: 'blur' }
          ],
          weldercard: [
            { required: true, message: '员工卡号不能为空', trigger: 'blur' }
          ],
          dept: [
            { required: true, message: '所属部门不能为空', trigger: 'blur' }
          ],
          groupname: [
            { required: true, message: '班组不能为空', trigger: 'blur' }
          ],
          techlevel: [
            { required: true, message: '技术级别不能为空', trigger: 'blur' }
          ],
          birthday: [
            { required: true, message: '出生日期不能为空', trigger: 'blur' }
          ],
          cardtype: [
            { required: true, message: '卡号类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.welderid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.welderid) {
            this.$http({
              url: this.$http.adornUrl(`/welderinfo/welderinfo/info/${this.dataForm.welderid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              // console.log(data)
              if (data && data.code === 0) {
                console.log(data)
                this.dataForm.weldername = data.welderinfo.weldername
                this.dataForm.weldercard = data.welderinfo.weldercard
                this.dataForm.dept = data.welderinfo.dept
                this.dataForm.groupname = data.welderinfo.groupname
                this.dataForm.techlevel = data.welderinfo.techlevel
                this.dataForm.birthday = data.welderinfo.birthday
                this.dataForm.cardtype = data.welderinfo.cardtype
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/welderinfo/welderinfo/${!this.dataForm.welderid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'welderid': this.dataForm.welderid || undefined,
                'weldername': this.dataForm.weldername,
                'weldercard': this.dataForm.weldercard,
                'dept': this.dataForm.dept,
                'groupname': this.dataForm.groupname,
                'techlevel': this.dataForm.techlevel,
                'birthday': this.dataForm.birthday,
                'cardtype': this.dataForm.cardtype
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
